<?php
function csv_to_array ($filename = '', $delimiter = ',')
{
    $header = null;
    $data = array();
    if (($handle = fopen($filename, 'r')) !== false)
    {
        while (($row = fgetcsv($handle, 1000, $delimiter)) !== false)
        {
            $data[] = $row;
        }
        fclose($handle);
    }
    return $data;
}

$lines = csv_to_array('list.csv', ';');
$naam = null;
$postcode = null;
$stad = null;

$xml = '<?xml version="1.0" encoding="iso-8859-1"?>
<root>';

foreach ($lines as $line)
{
    $naamPos = 2;
    $postcodePos = 9;
    $stadPos = 7;
    if (strlen(trim($line[$naamPos])) > 0)
    {
        $naam = trim($line[$naamPos]);
    }
    if (strlen(trim($line[$postcodePos])) > 0)
    {
        $postcode = trim($line[$postcodePos]);
    }
    if (strlen(trim($line[$stadPos])) > 0)
    {
        $stad = trim($line[$stadPos]);
    }
    $stad = ucwords(strtolower($stad));
//    $naam = trim(str_ireplace(" in ".$stad,"",$naam));
//    $naam = trim(str_ireplace(" in ".(substr($stad,0,-3)),"",$naam));
//    $naam = trim(str_ireplace($stad,"",$naam));
//    $naam = trim(str_ireplace(substr($stad,0,-3),"",$naam));
    $xml .= "
    <dealer>
		<naam><![CDATA[$naam]]></naam>
		<postcode>$postcode</postcode>
		<stad><![CDATA[$stad]]></stad>
	</dealer>";
}
$xml .= '</root>';
file_put_contents('list.xml', $xml);
